package com.zhuoli.service.springboot.mybatis.pagehelper.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhuoli.service.springboot.mybatis.pagehelper.common.ExampleFactory;
import com.zhuoli.service.springboot.mybatis.pagehelper.common.FieldInfo;
import com.zhuoli.service.springboot.mybatis.pagehelper.common.RelationEnum;
import com.zhuoli.service.springboot.mybatis.pagehelper.repository.mapper.UserMapper;
import com.zhuoli.service.springboot.mybatis.pagehelper.repository.model.User;
import com.zhuoli.service.springboot.mybatis.pagehelper.repository.model.UserExample;
import com.zhuoli.service.springboot.mybatis.pagehelper.repository.service.UserRepository;
import com.zhuoli.service.springboot.mybatis.pagehelper.service.UserControllerService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhuoli
 * @Date: 2018/8/12 12:30
 * @Description:
 */
@Service
@AllArgsConstructor
public class UserControllerServiceImpl implements UserControllerService {
    private UserRepository userRepository;

    private UserMapper userMapper;

    @Override
    public List<User> getAllUsers() {
        return userRepository.getAllUsers();
    }

    @Override
    public PageInfo<User> getByCondition(String userName, String description, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> queryResult = userRepository.getUserByCondition(userName, description);
        return new PageInfo<>(queryResult);
    }

    @Override
    public List<User> getTest(String userName, String description) {
        UserExample userExample = ExampleFactory.builder().add(new FieldInfo("UserName", userName, RelationEnum.LIKE.getValue()))
                .add(FieldInfo.builder().fieldName("Description").relation(RelationEnum.IS_NOT_NULL.getValue()).build())
                .setOrderByClause("id desc")
                .build().getExample(UserExample.class);

        return userMapper.selectByExample(userExample);
    }

    @Override
    public List<User> BetweenTest(String nameBegin, String nameEnd) {
        List<String> range = new ArrayList<>();
        range.add(nameBegin);
        range.add(nameEnd);
        PageHelper.startPage(1, 2);
        UserExample userExample = ExampleFactory.builder().add(new FieldInfo("UserName", range, RelationEnum.BETWEEN.getValue()))
                .add(FieldInfo.builder().fieldName("Description").relation(RelationEnum.IS_NOT_NULL.getValue()).build())
                .setOrderByClause("id desc")
                .build().getExample(UserExample.class);

        return userMapper.selectByExample(userExample);
    }

    @Override
    public User getUserById(Long id) {
        return userRepository.getUserById(id);
    }

    @Override
    public int delUserById(Long id) {
        return userRepository.delUserById(id);
    }

    @Override
    public int createUser(User user) {
        return userRepository.createUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userRepository.updateUser(user);
    }
}